<?php
/**
 * --------------------------------------------------------------
 * 内容控制器
 * --------------------------------------------------------------
 * Light Year Doc
 * @author yinq<3331653644@qq.com>
 * --------------------------------------------------------------
 */
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\admin\model\Document as DocumentModel;
use app\admin\model\Column as ColumnModel;
use util\Tree;
use think\Db;

class Content extends AdminBase
{
    /**
     * 内容列表
     */
	public function _empty()
	{
		cookie('__forward__', $_SERVER['REQUEST_URI']);
		$model_name = $this->request->action();
        $model      = Db::name('data_model')->where('en_title', $model_name)->find();
        if (!$model) $this->error('找不到该内容');
		
	    $keyword      = input('?post.keyword') ? input('post.keyword') : urldecode(input('get.keyword'));
		$search_field = input('?post.search_field') ? input('post.search_field') : urldecode(input('get.search_field'));
		$cid          = input('param.cid/d', 0);
		$start_time   = input('param.start_time/s', '');
		$end_time     = input('param.end_time/s', '');

		$map          = [];
        $conditions   = [
            'cid'          => $cid,
            'start_time'   => $start_time,
            'end_time'     => $end_time,
            'keyword'      => $keyword,
            'search_field' => $search_field
        ];
		
		// 独立模型
		if ($model['type'] == 2) {
		    $table_name = substr($model['table'], strlen(config('database.prefix')));
			// 查询
		    $map[] = [$table_name.'.model_id', '=', $model['id']];
			
			// 搜索条件
			if ($keyword) {
				$map[] = [$table_name.'.'.$search_field, 'like', '%'.$keyword.'%'];
			}
			if ($cid > 0) {
				$map[] = [$table_name.'.cid', '=', $cid];
			}
			if (!empty($start_time) && !empty($end_time) && $start_time == $end_time) {
				$map[] = [$table_name.'.create_time', '>=', strtotime($start_time." 00:00:00")];
				$map[] = [$table_name.'.create_time', '<=', strtotime($start_time." 23:59:59")];
			} else {
				if (!empty($start_time)) {
					$map[] = [$table_name.'.create_time', '>=', strtotime($start_time)];
				}
				if (!empty($end_time)) {
					$map[] = [$table_name.'.create_time', '<=', strtotime($end_time)];
				}
			}
			
			// 排序
            $order = $table_name.'.update_time desc';
			
            // 数据列表
            $list = Db::view($table_name, true)
                    ->view("column", 'title as column_title', 'column.id = '.$table_name.'.cid', 'left')
                    ->view("admin", 'adminname', 'admin.id = '.$table_name.'.aid', 'left')
                    ->where($map)
                    ->order($order)
                    ->paginate(config('app.site_setting.list_rows'), false, ['query' => $conditions]);
		} else {
			$map[] = ['document.model_id', '=', $model['id']];
			if ($keyword) {
				$map[] = ['document.'.$search_field, 'like', '%'.$keyword.'%'];
			}
			if ($cid > 0) {
				$map[] = ['document.cid', '=', $cid];
			}
			if (!empty($start_time) && !empty($end_time) && $start_time == $end_time) {
				$map[] = ['document.create_time', '>=', strtotime($start_time." 00:00:00")];
				$map[] = ['document.create_time', '<=', strtotime($start_time." 23:59:59")];
			} else {
				if (!empty($start_time)) {
					$map[] = ['document.create_time', '>=', strtotime($start_time)];
				}
				if (!empty($end_time)) {
					$map[] = ['document.create_time', '<=', strtotime($end_time)];
				}
			}
			$list = DocumentModel::view('document', true)
                    ->view("column", 'title as column_title', 'column.id = document.cid', 'left')
                    ->view("admin", 'adminname', 'admin.id = document.aid', 'left')
                    ->where($map)
                    ->order('document.update_time desc')
                    ->paginate(config('app.site_setting.list_rows'), false, ['query' => $conditions]);
		}
		
        $colmap = [
			'model_id' => $model['id'],
            'status'   => 1,
        ];
        $columns = ColumnModel::where($colmap)->order('id asc,sort asc')->column('id,title,pid');
        $columns = $columns ? object_to_array($columns) : null;
        $columns = $columns ? Tree::toFormatTree($columns) : [];
        $columns = array_merge(array(0 => array('id' => 0, 'title_show' => '请选择栏目')), $columns);
		
		$this->assign([
			'list'         => $list,
			'model'        => $model,
            'columns'      => $columns,
            'cid'          => $conditions['cid'],
            'start_time'   => $conditions['start_time'],
            'end_time'     => $conditions['end_time'],
            'keyword'      => $conditions['keyword'],
            'search_field' => $conditions['search_field'],
			'title'        => $model['title'] . '列表'
		]);
    	return $this->fetch('index');
	}

}